/*
 * @formatter:off
 * Copyright 2022 Cacao
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this common except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *        http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * @formatter:on
 */
package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * @author XuQx
 * @date 2022/08/25
 */
@EnableOpenApi
@Configuration(proxyBeanMethods = false)
public class ApplicationConfig {

    @Bean
    public Docket swaggerApi() {
        // 排除一些URL
        ApiInfo apiInfo = apiInfo();
        Docket docket = new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
            .paths((each) -> true)
            .build();
        // 写死需要token，不然需要添加许多配置做判断
        //docket.securityContexts(securityContexts());
        //docket.securitySchemes(securitySchemes());
        // 自定义解析，使用返回类型的泛型类型代替返回类型
        //docket.genericModelSubstitutes(decideModelClass());
        // knife4j的扩展信息
        //docket.extensions(openApiExtensionResolver.buildExtensions(apiInfo.getVersion()));
        return docket;
    }
    
    private ApiInfo apiInfo() {
        // 计算version，默认取服务的版本
        return new ApiInfoBuilder()
            .title("sw测试")
            .description("这是测试")
            .termsOfServiceUrl("http://xxxxxxx")
            .contact(new Contact("作者", "", ""))
            .version("1.0")
            .build();
    }
    
}
